package com.ecco.web;

import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Window;

import com.ecco.persistent.AccountAgentRemote;

public class MainWindow extends Window {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8453021958604400754L;
	private AccountAgentRemote dao;
	private static List accountList;
	
	
	public MainWindow() {
		System.out.println("Executing MainWindow's constructor.");
		setAccountList(new ArrayList());
		try {
			Context jndiContext = getInitialContext();
			Object ref = jndiContext.lookup("AccountAgentBean/remote");
			dao = (AccountAgentRemote)PortableRemoteObject.narrow(ref, AccountAgentRemote.class);
			setAccountList(dao.findAll());
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void onCreate() {
		System.out.println("This window event allows you to handle initialization events...");
		this.setTitle("Welcome to " + getAccountList().get(1));
	}
	
	public void showAvailableCourses(Treeitem item) {
		System.out.println("Showing tree of course titles...");
	}
	
	public static Context getInitialContext()
	throws NamingException {
	return new InitialContext();
	}

	public static void setAccountList(List accountList) {
		MainWindow.accountList = accountList;
	}

	public static List getAccountList() {
		return accountList;
	}

}
